﻿using WindowsForms.AOnline.Model.BusinessObjects.Dictionary;
using WindowsForms.AOnline.Presenter.Dictionary.Account;
using WindowsForms.AOnline.View.Dictionary;
//using System.Threading.Tasks;

namespace WindowsForms.AOnline.Presenter.Dictionary.Department
{
    public class DepartmentPresenter : Presenter<IDepartmentView>  
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AccountPresenter" /> class.
        /// </summary>
        /// <param name="view">The view.</param>
        public DepartmentPresenter(IDepartmentView view)
            : base(view)
        {
        }

        /// <summary>
        /// Displays the specified department identifier.
        /// </summary>
        /// <param name="departmentId">The department identifier.</param>
        public void Display(string departmentId) 
        {
            if (departmentId == null) { View.DepartmentId = 0; return; }
            var c = Model.GetDepartment(int.Parse(departmentId));
           // View.DepartmentId = department.DepartmentId;
          //  View.DepartmentName = department.Name;
            View.DepartmentId = c.DepartmentId;
            View.UnitId = c.UnitId;
            View.Code = c.Code;
            View.DepartmentName = c.Name;
            View.Address = c.Address;
            View.Description = c.Description;
            View.IsActive = c.IsActive;
            View.Fax = c.Fax;
            View.PhoneNumber = c.PhoneNumber;
            View.ParentId = c.ParentId;
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        /// <returns>System.Int32.</returns>
        public string Save()
        {
            var department = new DepartmentModel    
            {
                //DepartmentId = View.DepartmentId,
                DepartmentId = (int)View.DepartmentId,
                UnitId = View.UnitId,
                Code = View.Code,
                Name = View.DepartmentName,
                Address = View.Address,
                Description = View.Description,
                IsActive = View.IsActive,
                Fax = View.Fax,
                PhoneNumber = View.PhoneNumber,
                ParentId = View.ParentId,
            };

            if (View.DepartmentId == 0)
                return Model.AddDepartment(department);
            return Model.UpdateDepartment(department);
        }

        /// <summary>
        /// Deletes the specified department identifier.
        /// </summary>
        /// <param name="departmentId">The department identifier.</param>
        /// <returns>System.Int32.</returns>
        public string Delete(int departmentId) 
        {
            return Model.DeleteDepartment(departmentId);
        }
    }
}
